package com.example.myactspringboot.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.example.myactspringboot.util.ICustomProcessDiagramGenerator;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.PlatformTransactionManager;

import java.io.IOException;

/**
 * @Create 2023-09-13 22:39
 * @Description
 */
@Configuration
public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration implements ProcessEngineConfigurationConfigurer {
    @Autowired
    private ICustomProcessDiagramGenerator customProcessDiagramGenerator;

    /**
     * 解決工作流生成图片乱码问题
     *
     * @param processEngineConfiguration
     */
    @Override
    public void configure(SpringProcessEngineConfiguration processEngineConfiguration) {
        processEngineConfiguration.setActivityFontName("宋体");
        processEngineConfiguration.setAnnotationFontName("宋体");
        processEngineConfiguration.setLabelFontName("宋体");
        processEngineConfiguration.setProcessDiagramGenerator(customProcessDiagramGenerator);
    }
/*    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(DruidDataSource dataSource, PlatformTransactionManager platformTransactionManager){
        SpringProcessEngineConfiguration spec = new SpringProcessEngineConfiguration();
        spec.setDataSource(dataSource);
        spec.setTransactionManager(platformTransactionManager);
        spec.setDatabaseSchemaUpdate("true");
        Resource[] resources = null;
        // 启动自动部署流程
        try {
            resources = new PathMatchingResourcePatternResolver().getResources("classpath*:processes/*.bpmn");
        } catch (IOException e) {
            e.printStackTrace();
        }
        spec.setDeploymentResources(resources);
        return spec;
    }
    @Bean
    public ProcessEngineFactoryBean processEngine(SpringProcessEngineConfiguration engineConfiguration){
        ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
        processEngineFactoryBean.setProcessEngineConfiguration(engineConfiguration);
        return processEngineFactoryBean;
    }*/


    @Bean
    public RepositoryService repositoryService(ProcessEngineFactoryBean engineFactoryBean) throws Exception{
        return engineFactoryBean.getObject().getRepositoryService();
    }
    @Bean
    public RuntimeService runtimeService(ProcessEngineFactoryBean engineFactoryBean) throws Exception{
        return engineFactoryBean.getObject().getRuntimeService();
    }
    @Bean
    public TaskService taskService(ProcessEngineFactoryBean engineFactoryBean) throws Exception{
        return engineFactoryBean.getObject().getTaskService();
    }
    @Bean
    public HistoryService historyService(ProcessEngineFactoryBean engineFactoryBean) throws Exception{
        return engineFactoryBean.getObject().getHistoryService();
    }
}
